package com.bowen.demo.demo004;

import org.apache.commons.lang.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <h3>java-study</h3>
 * <p>验证邮箱合法性工具类</p>
 * @author : zhang.bw
 * @date : 2021-03-10 13:04
 **/
public class EmailUtils {

	/**
	 * 判断该邮件地址是否合法
	 * @param address 邮件地址，可以多个，逗号隔开
	 * @return
	 */
	public static boolean isEmailAddress(String address) {
		// 是否合法
		boolean flag = false;
		if (StringUtils.isEmpty(address)) {
			return false;
		}
		try {
			String[] addressArr = address.split(",");
			String check = "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
			Pattern regex = Pattern.compile(check);
			Matcher matcher = null;
			for (String str : addressArr) {
				matcher = regex.matcher(str);
				flag = matcher.matches();
				if (!flag) {
					return false;
				}
			}
		} catch (Exception e) {
			flag = false;
		}
		return flag;
	}
}
